package com.example.lanouhn.bantangapp.activities.activities;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.example.lanouhn.bantangapp.R;
import com.example.lanouhn.bantangapp.activities.contants.MyEvent;

import org.greenrobot.eventbus.EventBus;


/**
 * Created by lanouhn on 16/7/25.
 */
public class ShowDialog_Avatar_Name extends Activity {
    private TextView block;//关闭按钮
    private TextView preservation;//保存按钮
    private EditText et_name;//修改名称
    private EditText et_pname;//修改个性签名
    private ProgressBar progress_bar;//转圈圈
    //开启一个线程来控制转圈圈和中间视图保存成功的图片显示时间
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0) {
         // EventBus 传递数据
        EventBus.getDefault().post(new MyEvent(et_name.getText().toString(),et_pname.getText().toString()));

                finishActivity(199);

            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_avatar_photo_name);
        block = (TextView) findViewById(R.id.tv_guan_bi);
        preservation = (TextView) findViewById(R.id.tv_bao_cun);
        et_name = (EditText) findViewById(R.id.et_name);
        et_pname = (EditText) findViewById(R.id.et_pname);
        progress_bar = (ProgressBar) findViewById(R.id.progress_bar);
        initLinshener();
    }

    //监听点击
    private void initLinshener() {
        block.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setResult(101);

                finish();
            }
        });

        // 监听昵称输入框和个性签名输入框不为空的时候设置保存按钮点击的判断
        et_name.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (et_name.getText().toString().trim().length() > 0 && et_pname.getText().toString().trim().length() > 0) {
                    preservation.setTextColor(getResources().getColor(R.color.colorAccent));
                    preservation.setClickable(true);//设置保存按钮可以点击
                } else {
                    preservation.setTextColor(getResources().getColor(R.color.blank_gray));
                    preservation.setClickable(false);//设置保存按钮不能点击
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {
            }
        });
        // 监听个性签名输入框和昵称输入框不为空的时候设置保存按钮点击的判断
        et_pname.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                if (et_name.getText().toString().trim().length() > 0 && et_pname.getText().toString().trim().length() > 0) {
                    preservation.setTextColor(getResources().getColor(R.color.colorAccent));
                    preservation.setClickable(true);//设置保存按钮可以点击
                } else {
                    preservation.setTextColor(getResources().getColor(R.color.blank_gray));
                    preservation.setClickable(false);//设置保存按钮不能点击
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {
            }
        });
        //保存点击事件
        preservation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent it = new Intent(ShowDialog_Avatar_Name.this, ShowDialog_BaocunSucess.class);
                startActivityForResult(it, 199);
                //开启线程
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(2000);
                            handler.sendEmptyMessage(0);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
                progress_bar.setVisibility(view.getVisibility());

            }
        });
    }

    //与dialog中修改昵称和个性签名的联动 设置过后点击直接关闭dialog
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        finish();
    }

}
